package com.ruxiaoxin.share.persist.user.impl;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.ruxiaoxin.share.persist.user.UserRepository;
import com.ruxiaoxin.share.domain.user.User;

/**
 * @author ruxiaoxin
 */
@Repository
public class UserRepositoryImpl extends SqlSessionDaoSupport implements
		UserRepository {

	protected String getNamespace() {
		return "com.ruxiaoxin.User";
	}
	
	@Override
	public User getByEmail(String email) {
		return getSqlSession().selectOne(getNamespace() + ".getByEmail", email);
	}

	@Override
	public void save(User entity) {
		if(null == entity){
			throw new RuntimeException("entity can't be null");
		}
		getSqlSession().insert(getNamespace() + ".save", entity);		
	}

	@Override
	public void delete(User entity) {
		if(entity == null){
			throw new RuntimeException("id can't be null");
		}		
		getSqlSession().delete(getNamespace() + ".delete", entity);	
	}

	@Override
	public User get(String id) {
		if(id == null){
			throw new RuntimeException("id can't be null");
		}		
		return (User) getSqlSession().selectOne(getNamespace() + ".get", id);
	}

	@Override
	public void update(User entity) {
		if(null == entity){
			throw new RuntimeException("entity can't be null");
		}
		getSqlSession().update(getNamespace() + ".update", entity);	
	}

}
